﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SimSoDep
{
    public partial class frmNhapSoSimMoi : Form
    {
        // doi tuong ket noi
        private SqlConnection m_sqlConnection;
        // chuoi ket noi
        string m_strConnectionString;
        // chuoi select
        string m_strSelectString;
        // chuoi insert
        string m_strInsertString;
        // chuoi delete
        string m_strDeleteString;
        // chuoi update
        string m_strUpdateString;

        // SQL command: select du lieu
        SqlCommand m_SelectCommand;
        // SQL Command : Them du lieu
        SqlCommand m_InsertCommand;
        // SQL Command: Xoa du lieu
        SqlCommand m_DeleteCommand;
        // SQL Command: Sua du lieu
        SqlCommand m_UpdateCommand;
        // đối tượng cầu nối dữ liệu
        SqlDataAdapter m_DataAdapter;
        // đối tượng dataset
        DataSet m_dataSet;
        // đối tượng dataTable
        DataTable m_dataTable;
        public frmNhapSoSimMoi()
        {
          

            InitializeComponent();
            // khoi tao cac doi tuong 
            // chuỗi kết nối vào cơ sở dữ liệu
            m_strConnectionString = "Server=localhost;Integrated security=SSPI;database=QUANLYSIMSODEP";
            // câu lệnh truy vấn chọn dữ liệu
            m_strSelectString = "select * from SIMDEP";
            // doi tuong ket noi
            m_sqlConnection = new SqlConnection(m_strConnectionString);
            // doi tuong sqlCommand
            m_SelectCommand = new SqlCommand(m_strSelectString, m_sqlConnection);

            // doi tuong dataAdapter
            m_DataAdapter = new SqlDataAdapter();
            // doi tuong dataset
            m_dataSet = new DataSet();

            // doi tuong dataTable
            m_dataTable = new DataTable();

            // mở kết nối trong sự kiện khởi tạo
            try
            {
                m_sqlConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" lỗi kết nối" + ex.ToString());
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn thoát ?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Close();
            }
        }
       
        private void frmNhapSoSimMoi_Load(object sender, EventArgs e)
        {
            m_strSelectString = "select * from SIMDEP";
            m_sqlConnection = new SqlConnection(m_strConnectionString);

            try
            {
                m_sqlConnection.Open();
                m_SelectCommand = new SqlCommand(m_strSelectString, m_sqlConnection);
                m_DataAdapter = new SqlDataAdapter(m_SelectCommand);
                m_dataSet = new DataSet();
                m_dataTable = new DataTable();

                m_DataAdapter.Fill(m_dataSet);
                // Trích đối tượng DataTable từ DataSet.
                m_dataTable = m_dataSet.Tables[0];
                // dataGridView
                dataGridView1.DataSource = m_dataTable.DefaultView;

            }

            catch (Exception ex)
            {

                MessageBox.Show("Co loi trong qua trinh ket noi: " + ex.ToString());

            }
        }
        private void NhaMang()
        {
            if(cbxNhaMang.SelectedIndex==0)
              this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.viettel;
            if(cbxNhaMang.SelectedIndex==1)
                this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.vina;
            if (cbxNhaMang.SelectedIndex == 2)
                this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.mobi;
            if (cbxNhaMang.SelectedIndex == 3)
                this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.vietnammobi;
            if (cbxNhaMang.SelectedIndex == 4)
                this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.sfont;
            if (cbxNhaMang.SelectedIndex == 5)
                this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.beline;
           
        }
       

        private void cbxNhaMang_SelectedIndexChanged(object sender, EventArgs e)
        {
            NhaMang();
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            txtSoSim.Text = "";
            txtGiaTien.Text = "";
            cbxNhaMang.SelectedIndex = -1;
            this.ptbxNhaMang.BackgroundImage = global::SimSoDep.Properties.Resources.sim;

        }

       private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {

                // THUC HIEN CONG VIEC INSERT len giao dien dataGridView.
               DataRow dtRow = m_dataTable.NewRow();
                dtRow["MASIM"] = this.txtSoSim.Text;
                dtRow["NHAMANG"] = this.cbxNhaMang.SelectedItem;
                dtRow["GIA"] = this.txtGiaTien.Text;
                dtRow["LOAI"] = this.cbxLoaiSim.SelectedItem;
                m_dataTable.Rows.Add(dtRow);

                // khoi tao doi tuong m_strInsertString tai day
                if (this.txtSoSim.Text != "" && this.cbxNhaMang.SelectedIndex != -1 && this.txtGiaTien.Text != "" && this.cbxLoaiSim.SelectedIndex != -1)
                {
                    m_strInsertString = "insert into SIMDEP values('" + txtSoSim.Text + "','" + cbxNhaMang.SelectedItem + "', '"+ txtGiaTien.Text +"', '"+ cbxLoaiSim.SelectedItem +"')";
                }
                // khoi tao doi tuong m_InsertCommand o day
                m_InsertCommand = new SqlCommand(m_strInsertString, m_sqlConnection);

                // chọn SelectCommand của m_dataAdapter là m_SelectCommand
                this.m_DataAdapter.InsertCommand = m_InsertCommand;

                //  THUC HIEN CONG VIEC INSERT VAO CO SO DU LIEU.
                m_DataAdapter.Update(m_dataSet);
                // Application.DoEvents();


            }

            catch (Exception ex)
            {

                MessageBox.Show("Co loi trong qua trinh ket noi: " + ex.ToString());

            }

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {

                // THUC HIEN CONG VIEC DELETE.                
                 // so sanh tung dòng của m_dataTable
                // với txtSoSim.
                foreach (DataRow dtRow in m_dataTable.Rows)
                {
                    // neu trùng
                    if (this.txtSoSim.Text == dtRow["MASIM"].ToString())
                    {
                        // xoa dtRow ra khoi m_dataTable.
                        dtRow.Delete();
                    }
                }

                // khoi tao doi tuong m_strInsertString tai day
                if (this.txtSoSim.Text != "" && this.cbxNhaMang.SelectedIndex != -1 && this.txtGiaTien.Text != "" && this.cbxLoaiSim.SelectedIndex != -1)
                {

                    m_strDeleteString = "Delete from SIMDEP where MASIM ='" + this.txtSoSim.Text + "'";
                }
                // khoi tao doi tuong m_DeleteCommand o day
                m_DeleteCommand = new SqlCommand(m_strDeleteString, m_sqlConnection);

                // chọn DeleteCommand của m_dataAdapter là m_DeleteCommand
                this.m_DataAdapter.DeleteCommand = m_DeleteCommand;

                //  THUC HIEN CONG VIEC Delete VAO CO SO DU LIEU.
                m_DataAdapter.Update(m_dataSet);

            }

            catch (Exception ex)
            {

                MessageBox.Show("Co loi trong qua trinh ket noi: " + ex.ToString());

            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {

                // THUC HIEN CONG VIEC UPDATE.                
                  // cap nhat lai m_dataTable.
                foreach (DataRow dtRow in m_dataTable.Rows)
                {
                    // neu trùng
                    if (this.txtSoSim.Text == dtRow["MASIM"].ToString())
                    {
                        // nhung cot can sua
                        dtRow["NHAMANG"] = this.cbxNhaMang.SelectedItem;
                        dtRow["GIA"] = this.txtGiaTien.Text;
                        dtRow["LOAI"] = this.cbxLoaiSim.SelectedItem;
                    }
                }

                // khoi tao doi tuong m_strUpdateString tai day
                if (this.txtSoSim.Text != "")
                {

                    m_strUpdateString = " Update SIMDEP set NHAMANG ='" + this.cbxNhaMang.SelectedItem + "',GIA = '"+ this.txtGiaTien.Text+"', LOAI = '"+ this.cbxLoaiSim.SelectedItem+"' where MASIM='" + this.txtSoSim.Text+ "'";
                }
                // khoi tao doi tuong m_UpdateCommand o day
                m_UpdateCommand = new SqlCommand(m_strUpdateString, m_sqlConnection);

                // chọn UpdateCommand của m_dataAdapter là m_UpdateCommand
                this.m_DataAdapter.UpdateCommand = m_UpdateCommand;

                //  THUC HIEN CONG VIEC Update VAO CO SO DU LIEU.
                m_DataAdapter.Update(m_dataSet);



            }

            catch (Exception ex)
            {

                MessageBox.Show("Co loi trong qua trinh ket noi: " + ex.ToString());
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Tìm cách lấy chỉ số dòng của Ô được chọn
            int iRowIndex = -1;
            if (this.dataGridView1.SelectedCells.Count > 0)
            {
                iRowIndex = this.dataGridView1.SelectedCells[0].RowIndex;
            }

            if (iRowIndex >= 0)
            {
                DataGridViewRow dtRow = this.dataGridView1.Rows[iRowIndex];
                if (dtRow.Cells[0].Value != null)
                {
                    this.txtSoSim.Text = dtRow.Cells[0].Value.ToString();
                    this.cbxNhaMang.SelectedItem = dtRow.Cells[1].Value.ToString();
                    this.txtGiaTien.Text = dtRow.Cells[2].Value.ToString();
                    this.cbxLoaiSim.SelectedItem = dtRow.Cells[3].Value.ToString();
                }
            }
        }

     }
}